<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------
// 地区配置
class BusinessCircleAction extends CommonAction{
	public function index()
	{
		$city_name = trim($_REQUEST['city_name']);
		$parent_name = trim($_REQUEST['parent_name']);

		$where = '';

		$parameter = array();
		if(!empty($city_name))
		{
			$this->assign("city_name",$city_name);
			$where.=" AND gc.name = '$city_name'";
			$parameter['city_name'] = $city_name;
		}

		if(!empty($parent_name))
		{
			$this->assign("parent_name",$parent_name);
			$where.=" and bcp.name = '$parent_name'";
			$parameter['parent_name'] = $parent_name;
		}

		$model = M();

		$count_str = 'SELECT COUNT(bc.id) AS bcount
				FROM '.C("DB_PREFIX").'business_circle AS bc
				INNER JOIN '.C("DB_PREFIX").'group_city AS gc ON gc.id = bc.city_id
				LEFT JOIN '.C("DB_PREFIX").'business_circle AS bcp ON bcp.id = bc.parent_id
				where 1 '.$where;

		$count = $model->query($count_str);
		$count = $count[0]['bcount'];

		$sql_str = 'SELECT bc.id,bc.name,gc.name AS city_name,bcp.name AS parent_name,bc.sort
				FROM '.C("DB_PREFIX").'business_circle AS bc
				INNER JOIN '.C("DB_PREFIX").'group_city AS gc ON gc.id = bc.city_id
				LEFT JOIN '.C("DB_PREFIX").'business_circle AS bcp ON bcp.id = bc.parent_id
				where 1 '.$where;

		$voList = $this->_sqlList($model, $sql_str,$count, $parameter, 'id', true);
		$this->display();
	}

	public function edit()
	{
       	$name = $this->getActionName();
		$model = M($name);
		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById($id);

        $city_list=D("GroupCity")->where("status=1")->findAll();
		$area_list=D("BusinessCircle")->where("parent_id = 0 AND id <> $id AND city_id = ".$vo['city_id'])->findAll();

		$this->assign('city_list',$city_list);
		$this->assign('area_list',$area_list);

		$this->assign('vo', $vo);
		$this->display();
    }

        //增
	public function add()
	{
		$city_list=D("GroupCity")->where("status=1")->findAll();
		$area_list=array();

		$this->assign('city_list',$city_list);
		$this->display ();
	}

	function insert()
	{
		$name=$this->getActionName();
		$model = D ($name);
		$data = $model->create ();
		if ($data === false) {
			$this->error ( $model->getError () );
		}

		if($model->where("name = '$data[name]' AND parent_id = $data[parent_id] AND city_id = $data[city_id]")->count() > 0)
		{
			if($data['parent_id'] == 0)
				$this->error('该城市下已有该商圈');
			else
				$this->error('该城市商圈下已有该子商圈');
		}

		$list=$model->add ($data);
		//dump($model->getLastSql());
		if ($list!==false) { //保存成功

			$this->saveLog(1,$list);
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}

	function update()
	{
		$name=$this->getActionName();
		$model = D ($name);
		$data = $model->create ();
		if (false === $data) {
			$this->error ( $model->getError () );
		}

		// 更新数据
		$list=$model->save ($data);
		if (false !== $list) {
			//成功提示
			$this->saveLog(1);
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error (L('EDIT_FAILED'));
		}
	}

	//永久删除
	public function foreverdelete()
	{
		//删除指定记录
		$model = D (MODULE_NAME);
		if (! empty ( $model ))
		{
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			$ids = explode ( ',', $id );
			$names = '';
			$name_arr = array();

			foreach($ids as $id)
			{
				$city = M("BusinessCircle")->where("id=".$id)->field("name")->find();
				$name_arr[] = $city['name'];
				$names .= $city['name'].",";
			}

			if($names!='')
			{
				$names = substr($names,0,strlen($names)-1);
			}

			if (count($ids) > 0)
			{
                $condition = array ('parent_id' => array ('in',$ids));
                if($model->where($condition)->count() > 0)
                    $this->error('商圈下存在子商圈,请先删除子商圈');

				$condition = array ($pk => array ('in',$ids));
				if (false !== $model->where ( $condition )->delete())
				{
					D("GoodsArea")->where(array('area' => array('in',$name_arr)))->delete();

					$msg = L('DELETE_MSG').$names;
					$this->saveLog(1,0,$msg);
					$this->success ( L('DEL_SUCCESS') );
				}
				else
				{
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}

	public function getArea()
	{
		$result['status'] = 1;
		$city_id = intval($_REQUEST['city_id']);
		$result['list']=D("BusinessCircle")->where("city_id = $city_id AND parent_id = 0")->findAll();
		echo json_encode($result);
	}

	public function getChildArea()
	{
		$result['status'] = 1;
		$city_id = intval($_REQUEST['city_id']);
		$parent_name = trim($_REQUEST['parent_name']);

		$parent_id = D("BusinessCircle")->where("city_id = $city_id AND name = '$parent_name'")->getField('id');
		$result['list']=D("BusinessCircle")->where("parent_id = ".$parent_id)->findAll();
		echo json_encode($result);
	}
}
?>